{extend name="../../base/view/common/base" /}
<!-- 主体 -->
{block name="body"}
<link href="{__STATIC__}/home/other/formSelects-v4.css" rel="stylesheet" />
<script src="{__STATIC__}/home/other/jquery.min.js"></script>
<script src="{__STATIC__}/home/other/formSelects-v4.min.js"></script>
<style>
    .layui-tree-entry{font-size:15px; line-height:24px}
    .layui-tree-set{padding:2px 0}
    .layui-tree-iconClick .layui-icon{color:#1E9FFF}
    .layui-icon layui-icon-file{font-size:16px;}
    .layui-tree-icon {height: 15px;width: 15px; text-align: center;border: 1px solid #1E9FFF; color:#1E9FFF}
    .layui-tree-line .layui-tree-set .layui-tree-set:after{top:18px;}
    .tree-left{width:200px; float:left; height:calc(100% - 30px); overflow: scroll; border:1px solid #eeeeee; background-color:#FAFAFA; padding:12px 12px 12px 5px;}
    .tree-left h3{font-size:16px; height:30px; padding-left:10px; font-weight:800}

    .gougu-upload-files{background-color: #ffffff; border:1px solid #e4e7ed;color: #c0c4cc;cursor: not-allowed; padding:0 12px; width:180px; box-sizing: border-box; display: inline-block; font-size: inherit; height: 38px; line-height: 35px; margin-right:8px; border-radius:2px;}
    .gougu-upload-tips{color:#969696}
    .layui-form-item{margin-bottom:8px;}
    .layui-input-block{min-height:24px;}
</style>
<div class="layui-col-lg12 layui-col-md12 layui-col-sm12">
    <div class="layui-col-lg3 layui-col-md3 layui-col-sm3 layui-col-xl3 layui-col-sm3 layui-col-xs3 layui-padding-3">
        <h3>物料分类</h3>
        <div id="ID-tree-demo-showLine"></div>
    </div>
    <div class="layui-col-lg9 layui-col-md9 layui-col-xl9 layui-col-sm9 layui-col-sm9 layui-col-xs9">
        <form class="layui-form gg-form-bar border-t border-x">
            <div class="layui-input-inline" style="width:300px;">
                <input type="text" name="bar_code" placeholder="物料编码" class="layui-input" autocomplete="off" />
            </div>
             <div class="layui-input-inline" style="width:300px;">
                <input type="text" name="mdName" placeholder="物料名称" class="layui-input" autocomplete="off" />
            </div>
             <div class="layui-input-inline" style="width:300px;">
                <input type="text" name="specs" placeholder="规格型号" class="layui-input" autocomplete="off" />
            </div>
            <div class="layui-input-inline" style="width:300px;">
                <select name="warehouse_id" lay-search  xm-select="warehouse_id" lay-filter="warehouse_id">
                    <option value="">仓库</option>
                    {volist name='warehouseList' id='wl'}
                    <option value="{$wl.id}" >{$wl.name}</option>
                    {/volist}
                </select>
            </div>
            <div class="layui-input-inline" style="width:300px;">
                <select name="locator" id="locator" lay-search  xm-select="locator" lay-filter="locator" >
                    <option value="">请选先选择仓库</option>
                </select>
            </div>

            <div class="layui-input-inline" style="width:150px;">
                <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="webformIndex"><i class="layui-icon layui-icon-search mr-1"></i>搜索</button>
                <button type="reset" class="layui-btn layui-btn-reset" lay-filter="reset">清空</button>
            </div>
        </form>
        <div class="p-3">
            <div>
                <table class="layui-hide" id="treeTable" lay-filter="treeTable"></table>
            </div>
        </div>
    </div>
</div>

<script type="text/html" id="toolbarDemo">
</script>
<script type="text/html" id="actionBar">

</script>
{/block}
<!-- /主体 -->

<!-- 脚本 -->
{block name="script"}
<script type="text/javascript">
    formSelects.render('warehouse_id');
    formSelects.btns('warehouse_id',[]);
    formSelects.on('warehouse_id',function (id,data) {
        let ids=[];
        data.forEach(function (item,index) {
            ids.push(item.value)
        })
           getLocal(ids);
    },true)

</script>
<script>
    const moduleInit = ['tool'];
    function gouguInit() {
        var treeTable = layui.treeTable, tool = layui.tool,form = layui.form;
        var upload=layui.upload;
        var tableIns = treeTable.render({
            elem: '#treeTable'
            , url: "{:url('index')}"
            ,toolbar: '#toolbarDemo',
            defaultToolbar:  ['filter',  'exports']
            , tree: { // treeTable 特定属性集
                customName: {name: 'name'},
                data: {},
                view: {showIcon: false},
                async: {},
                callback: {}
            }
            , cellMinWidth: 80
            , cols: [[
                {type: 'checkbox', fixed: 'left' },
                {fixed: 'left',field: 'id',title: 'ID' ,unresize: true,hide:true},
                {field: 'id', width: 80, title: 'ID号', align: 'center'}
                , {field: 'mdName',  title: '物料名称'}
                , {field: 'specs',  title: '规格型号'}
                , {field: 'unit',  title: '单位'}
                , {field: 'warehouseName',  title: '仓库'}
                , {field: 'locatorName',  title: '库位'}
                       , {field: 'hasNum',  title: '数量'}
                // , {title: '操作',fixed: 'right',width: 200,  align: 'center'
                //     , toolbar: '#actionBar'
                // }
            ]]
            , page: false
        });
        //监听搜索提交
        form.on('submit(webformIndex)', function (data) {
            tableIns.reload({
                where: data.field,
                page: {
                    curr: 1
                }
            });
            return false;
        });
        //表头工具栏事件
        $('.add-menu').on('click', function () {
            tool.side("/home/BomForm/add");
            return;
        });

        //操作按钮
        treeTable.on('tool(treeTable)', function (obj) {
            console.log(obj);
            if (obj.event === 'add') {
                tool.side('/home/BomForm/add?pid=' + obj.data.id);
                return;
            }
            if (obj.event === 'edit') {
                tool.side('/home/BomForm/add?id=' + obj.data.id);
                return;
            }
            if (obj.event === 'del') {
                layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            obj.del();
                        }
                    }
                    tool.delete("/home/BomForm/delete", {id: obj.data.id}, callback);
                    layer.close(index);
                });
            }
            if(obj.event === 'forbid'){
                layer.confirm('确定要禁用吗?', {icon: 3, title:'提示'}, function(index){
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            tableIns.reload();
                        }
                    }
                    tool.post("/home/BomForm/doCheck", { id: obj.data.id,status: 2}, callback);
                    layer.close(index);
                });
            }
            if(obj.event === 'resume'){
                layer.confirm('确定要启用吗?', {icon: 3, title:'提示'}, function(index){
                    let callback = function (e) {
                        layer.msg(e.msg);
                        if (e.code == 0) {
                            tableIns.reload();
                        }
                    }
                    tool.post("/home/BomForm/doCheck", { id: obj.data.id,status: 1}, callback);
                    layer.close(index);
                });
            }
        });
        treeTable.on('toolbar(treeTable)', function(obj){
            var checkStatus = treeTable.checkStatus(obj.config.id);
            console.log(checkStatus)
            switch(obj.event){
                case 'add':
                    break;
                case 'dels':
                    var data = checkStatus.data;
                    var ids=[]
                    if (data.length){
                        data.forEach(function (value,index) {
                            console.log('value',value)
                            ids.push(value.id)
                        })
                        layer.confirm('确定要删除吗?', {icon: 3, title: '提示'}, function (index) {
                            let callback = function (e) {
                                layer.msg(e.msg);
                                if (e.code == 0) {
                                    tableIns.reload();
                                }
                            }
                            tool.delete("/home/BomForm/delete", {id: ids.join(',')}, callback);
                            layer.close(index);
                        });

                    }else {
                        layer.msg('请选择数据');
                    }

                    break;
                case 'import':

                    break;

            };
        })
        var tree = layui.tree;
        $.ajax({
            url: "/home/api/getMaterial_type",
            type:'post',
            success:function(res){
                //仅节点左侧图标控制收缩
                tree.render({
                    elem: '#ID-tree-demo-showLine',
                    data: res.data,
                    onlyIconControl: true,  //是否仅允许节点左侧图标控制展开收缩
                    click: function(obj){
                        //layer.msg(JSON.stringify(obj.data));
                        tableIns.reload({
                            where: {type: obj.data.id}
                            ,page:{curr:1}
                        });
                        // $('[name="keywords"]').val('');
                        // $('[name="status"]').val('');
                        // $('[name="type"]').val('');
                        layui.form.render('select');
                    }
                });
            }
        })

    }

function getLocal(id) {
    $("#locator").empty();
        if (id.length){
            ids=id.join(',')
            var optionStr="<option value=''>请选择数据</option>";
            $.ajax({
                url:'/home/api/getWarHoseDetail',
                type:'post',
                dataType:'json',
                data:{id:ids},
                success:function (res) {
                    if (res.code==0){
                        let data=res.data;
                        for(var i=0;i<data.length;i++){
                            optionStr+=" <option value='"+data[i].id+"'>"+data[i].name+"</option>";
                        }
                    }else {
                    }
                    $("#locator").append(optionStr);
                    formSelects.render('locator');
                    formSelects.btns('locator',[]);
                }

            })
        }else {
            $("#locator").append(" <option value=''>请选先选择仓库</option>");
            formSelects.render('locator');
            formSelects.btns('locator',[]);
        }


}

</script>
{/block}
<!-- /脚本 -->